/**
 * Project: shuyin-sync
 * 
 * File Created at Mar 15, 2012
 * $Id$
 * 
 * Copyright 2008 6677bank.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * 6677bank Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with 6677bank.com.
 */
package com.bank6677.sync.csm.helper;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.bank6677.common.exception.BusinessException;
import com.bank6677.sync.csm.domain.old.Rule;
import com.bank6677.uambank.model.role.Role;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * UM用户帮助类
 * 
 * @author liyue
 * @version UMUserHelper.java Mar 15, 2012 10:18:56 AM
 */
@Repository
public class UMUserHelper {

    public static final Integer       MALE            = 0;
    public static final Integer       FEMALE          = 1;

    public final static Integer       ACTIVE_USER     = 0;                           //激活用户
    public final static Integer       NOT_ACTIVE_USER = 1;                           //未激活用户

    private static Map<Integer, Role> umRoleMap       = new HashMap<Integer, Role>();

    @Autowired
    @Qualifier("newpayxSqlMapClient")
    private SqlMapClient              newpayxClient;

    @Autowired
    @Qualifier("uamSqlMapClient")
    private SqlMapClient              uamClient;

    /**
     * 根据老用户的用户性别得到新的用户性别
     * 
     * @param oldUserSex
     * @return
     */
    public static Integer getSex(Integer oldUserSex) {
        return OldUserHelper.MALE.equals(oldUserSex) ? MALE : FEMALE;
    }

    /**
     * 根据老用户的用户状态得到新的用户状态
     * 
     * @param oldUserStatus
     * @return
     */
    public static Integer getStatus(Integer oldUserStatus) {
        return OldUserHelper.ACTIVE_USER.equals(oldUserStatus) ? ACTIVE_USER : NOT_ACTIVE_USER;
    }

//    @PostConstruct
    private void initUmRoleMap() {
        try {
            List<Role> umRoles = uamClient.queryForList("user.findAllUmRoles");
            List<Rule> oldRoles = newpayxClient.queryForList("user.findAllOldRoles");

            Map<Integer, Integer> roleMapping = new HashMap<Integer, Integer>();
            roleMapping.put(12, 13);
            roleMapping.put(34, 21);
            roleMapping.put(25, 15);
            
            for (Rule rule : oldRoles) {
                for (Role role : umRoles) {
                    if (new Integer(role.getId()).equals(roleMapping.get(rule.getId()))) {
                        umRoleMap.put(rule.getId(), role);
                    }
                }
            }
        } catch (SQLException e) {
            throw new BusinessException("初始化角色信息时出错！！", e);
        }
    }

    /**
     * 取得新的UM系统中的对应角色ID
     * 
     * @param roleId 老系统角色id
     */
    public static Role getUmRoleByOldRoleId(Integer roleId) {
        return umRoleMap.get(roleId);
    }
}
